package com.example.server.controller;

import com.example.server.api.dto.ElasticCityDto;
import com.example.server.api.service.ElasticManager;
import com.example.server.model.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ElasticController {

    @DubboReference
    private ElasticManager elasticManager;

    @PostMapping("/add-city")
    ElasticCityDto addCity(ElasticCityDto city){
        return this.elasticManager.addCity(city);
    }

    @PostMapping("/add-city-v2")
    boolean addCityV2(ElasticCityDto city){
        return this.elasticManager.addCityV2(city);
    }

    @GetMapping("/list-city")
    Result<List<ElasticCityDto>> listCity(){
        // 自定义异常用法
//        try{
//            int a = 1/0;
//        }catch (ArithmeticException e){
//            e.printStackTrace();
//            throw new CustomException(ResultCodes.DIVIDENDISZEROERROR);
//        }
        return Result.succeed(this.elasticManager.listCity());
    }

    @GetMapping("/list-city-v2")
    List<ElasticCityDto> listCityV2(){
        return this.elasticManager.listCity();
    }

    @GetMapping("/top/{num}")
    List<ElasticCityDto> top(@PathVariable Integer num){
        if (num == null) num = 10;
        return this.elasticManager.top(num);
    }

}
